package com.smec.remes.module.query.mantr;

import android.app.ProgressDialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.activity.ArrayListStock;
import com.smec.moblie.databinding.QueryNumberBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.query.ListQueryMantr;
import com.smec.remes.bean.query.QueryMantr;
import com.smec.remes.module.query.mantr.adapter.SmecQueryMantrAdapter;
import com.smec.remes.module.submit.SmecSubmitPresenter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.GetFullUrl;
import com.smec.remes.util.ScreenListener;
import com.smec.remes.util.WmsConstant;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/4.
 */

public class SmecMantrFragment extends SmecBaseFragment<SmecSubmitPresenter> {

    private QueryNumberBinding queryNumberBinding;
    private String _url="WmsGetMatnrByDwgnoPS";
    private Gson gson = new Gson();
    private ProgressDialog progressDialog;
    private Boolean diaLog=false;
    private Boolean senior=false;
    private SmecQueryMantrAdapter smecQueryMantrAdapter;
    private ScreenListener screenListener ;
    private Boolean lock=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        queryNumberBinding=DataBindingUtil.inflate(inflater, R.layout.query_number,container,false);
        smecQueryMantrAdapter=new SmecQueryMantrAdapter(getActivity());
        queryNumberBinding.queryMantrRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        queryNumberBinding.queryMantrRecyclerView.setAdapter(smecQueryMantrAdapter);
        _choiceSenior();
        _Reset();
        _Query();
        return queryNumberBinding.getRoot();
    }


    /**
     *  查询
     * @return
     */
    public void _Query(){
        queryNumberBinding.btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(queryNumberBinding.edPicture.getText().toString())){
                    Toast.makeText(getActivity(),"请输入图号",Toast.LENGTH_SHORT).show();
                }else {
                    senior=true;
                    CommonUtils.hideImmManager(queryNumberBinding.getRoot());
                    _getList();
                    if(progressDialog == null){
                        progressDialog=new ProgressDialog(getActivity());
                    }
                    progressDialog.setMessage("正在查询.....");
                    progressDialog.show();
                    queryNumberBinding.queryMantrRecyclerView.smoothScrollToPosition(0);
                    smecQueryMantrAdapter._clear();
                }
            }
        });

        queryNumberBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(queryNumberBinding.edSearch.getText().toString())){
                    senior=false;
                    CommonUtils.hideImmManager(queryNumberBinding.getRoot());
                    _getList();
                    if(progressDialog == null){
                        progressDialog=new ProgressDialog(getActivity());
                    }
                    progressDialog.setMessage("正在查询.....");
                    progressDialog.show();
                    queryNumberBinding.queryMantrRecyclerView.smoothScrollToPosition(0);
                    smecQueryMantrAdapter._clear();
                }else {
                    Toast.makeText(getActivity(),"请输入图号",Toast.LENGTH_SHORT).show();
                }
            }
        });

        queryNumberBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    if(!TextUtils.isEmpty(queryNumberBinding.edSearch.getText().toString())){
                        senior=false;
                        CommonUtils.hideImmManager(queryNumberBinding.getRoot());
                        _getList();
                        if(progressDialog == null){
                            progressDialog=new ProgressDialog(getActivity());
                        }
                        progressDialog.setMessage("正在查询.....");
                        progressDialog.show();
                        queryNumberBinding.queryMantrRecyclerView.smoothScrollToPosition(0);
                        smecQueryMantrAdapter._clear();
                    }else {
                        Toast.makeText(getActivity(),"请输入图号",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

    }

    /**
     *  调接口
     */
    public void _getList(){
        HashMap hashMap=new HashMap();
        if(senior){
            hashMap.put("DWGNO",_getEd(queryNumberBinding.edPicture.getText().toString()));
            hashMap.put("LCODE",_getEd(queryNumberBinding.edL.getText().toString()));
            hashMap.put("GCODE",_getEd(queryNumberBinding.edG.getText().toString()));
            hashMap.put("REMARK",_getEd(queryNumberBinding.edParameter.getText().toString()));
        }else {
            hashMap.put("DWGNO",queryNumberBinding.edSearch.getText().toString());
        }
        String adress= GetFullUrl.getFullUrl(_url,hashMap);
        Log.w("URL", GetFullUrl.getFullUrl(_url,hashMap));
        OkHttpUtils.get().url(adress).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                //返回无数据  页面更新
                progressDialog.dismiss();
                ListQueryMantr listQueryMantr=gson.fromJson(response.toString().trim(),ListQueryMantr.class);
                ArrayList<QueryMantr> arrayList=new ArrayList<>();
                if(CommonUtils.notEmpty(listQueryMantr.getData())){
                    for(int i=0;i<listQueryMantr.getData().size();i++){
                        arrayList.add(listQueryMantr.getData().get(i));
                    }
                    smecQueryMantrAdapter.setArrayList(arrayList);
                }
            }
        });
    }

    public String _getEd(String a){
        if(TextUtils.isEmpty(a)){
            return "";
        }else {
            return a;
        }
    }

    /**
     *  选择是否高级
     * @return
     */
    public void _choiceSenior(){
        queryNumberBinding.llListSeniorYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryNumberBinding.llSeniorYes.setVisibility(View.GONE);
                queryNumberBinding.llSeniorNo.setVisibility(View.VISIBLE);
            }
        });

        queryNumberBinding.llListSeniorNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryNumberBinding.llSeniorYes.setVisibility(View.VISIBLE);
                queryNumberBinding.llSeniorNo.setVisibility(View.GONE);
            }
        });
    }

    public void _Reset(){
        queryNumberBinding.btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryNumberBinding.edPicture.setText("");
                queryNumberBinding.edG.setText("");
                queryNumberBinding.edL.setText("");
                queryNumberBinding.edParameter.setText("");
                queryNumberBinding.edPicture.requestFocus();
                queryNumberBinding.edPicture.requestFocusFromTouch();
                queryNumberBinding.edG.clearFocus();
                queryNumberBinding.edL.clearFocus();
                queryNumberBinding.edParameter.clearFocus();
                queryNumberBinding.invalidateAll();
            }
        });
        queryNumberBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryNumberBinding.edSearch.setText("");
            }
        });
        queryNumberBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                queryNumberBinding.ivDelete.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        queryNumberBinding.edSearch.setText("");
        queryNumberBinding.edPicture.setText("");
        queryNumberBinding.edG.setText("");
        queryNumberBinding.edL.setText("");
        queryNumberBinding.edParameter.setText("");
    }

    @Override
    protected SmecSubmitPresenter getPersenter() {
        return null;
    }

    public static SmecMantrFragment newInstance() {
        Bundle args = new Bundle();
        SmecMantrFragment fragment = new SmecMantrFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
